package com.zeus.base.smscenter.service.impl;

import com.zeus.base.privilege.utils.ResultModel;
import com.zeus.base.smscenter.mapper.SupplierMapper;
import com.zeus.base.smscenter.model.SmsSupplier;
import com.zeus.base.smscenter.model.SmsSupplierProperty;
import com.zeus.base.smscenter.service.ISupplierPropertyService;
import com.zeus.base.smscenter.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 短信项目设定Service
 */
@Service
@Transactional(readOnly = true)
public class SupplierServiceImpl implements ISupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    @Autowired
    private ISupplierPropertyService iSupplierPropertyService;

    @Override
    @Transactional(readOnly = true)
    public List<SmsSupplier> findList(SmsSupplier supplier) {
        List<SmsSupplier> list = supplierMapper.findList(supplier);
        return list;
    }

    @Override
    @Transactional(readOnly = true)
    public SmsSupplier selectByCode(String code) {

        return supplierMapper.selectByCode(code);
    }

    /**
     * 获取供应商信息(租户code,供应商,平台)
     *
     * @param smsSupplier

     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public SmsSupplier existByCodeSuppNoAppForm(SmsSupplier smsSupplier) {
        return supplierMapper.existByCodeSuppNoAppForm(smsSupplier);
    }

    @Override
    @Transactional(readOnly = true)
    public SmsSupplier selectByPrimaryKey(Integer id) {

        return supplierMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = true)
    public SmsSupplier selectByPrimaryKey(String id) {
        return supplierMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = true)
    public SmsSupplier get(SmsSupplier entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel insert(SmsSupplier entity) {
        ResultModel result = exists(entity);
        if (result.isSuccess()) {
            int r = supplierMapper.insert(entity);
            if (r == 0)
                result = new ResultModel(false, "操作保存失败");
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel insertSelective(SmsSupplier entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel update(SmsSupplier entity) {
        ResultModel result = exists(entity);
        if (result.isSuccess()) {
            int r = supplierMapper.updateByPrimaryKey(entity);
            if (r == 0)
                result = new ResultModel(false, "操作保存失败");
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel updateSelective(SmsSupplier entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(Integer id) {
        int r = supplierMapper.deleteByPrimaryKey(id);
        if (r == 0)
            return new ResultModel(false, "操作保存失败");
        return new ResultModel(true, "操作保存成功");
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(SmsSupplier entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = true)
    public ResultModel exists(SmsSupplier entity) {
        List<SmsSupplier> list = null;//tenantMapper.exists(entity);
        if (list == null || list.size() == 0)
            return new ResultModel(true, "操作保存成功");
        for (SmsSupplier t : list) {
            return new ResultModel(false, "消费项目名称已经存在");
        }
        return new ResultModel(true, "操作保存成功");
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel deleteByKeyId(String keyId) {
        List<String[]> failedIds = new ArrayList<String[]>();
        SmsSupplier entity = null;
        SmsSupplierProperty smsSupplierProperty = new SmsSupplierProperty();
        entity = selectByPrimaryKey(keyId);
        smsSupplierProperty.setTenantCode(entity.getTenantCode());
        smsSupplierProperty.setSupplierNo(entity.getSupplierNo());
        //删除供应商前,先把供应商的属性信息删除
        ResultModel ml = iSupplierPropertyService.deleteByCodeSuppNo(smsSupplierProperty);
        if(ml.isSuccess()) {
            //删除供应商信息
            int r = supplierMapper.deleteByKeyId(entity);
            if (r==0) {
                failedIds.add(new String[]{keyId, "数据删除失败!"});
            }
        }
        return new ResultModel(true, 0, "", failedIds);
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel batchDelete(String[] keyIds) {
        List<String[]> failedIds = new ArrayList<String[]>();
        SmsSupplier entity = null;
        SmsSupplierProperty smsSupplierProperty = new SmsSupplierProperty();
        for (String id : keyIds) {
            entity = selectByPrimaryKey(id);
            smsSupplierProperty.setTenantCode(entity.getTenantCode());
            smsSupplierProperty.setSupplierNo(entity.getSupplierNo());
            //删除供应商前,先把供应商的属性信息删除
            iSupplierPropertyService.deleteByCodeSuppNo(smsSupplierProperty);
            //删除供应商信息
            int r = supplierMapper.deleteByKeyId(entity);
            if (r==0) {
                failedIds.add(new String[]{id, "数据删除失败!"});
            }
        }
        return new ResultModel(true, 0, "", failedIds);
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel deleteByTenantCode(SmsSupplier smsSupplier) {
        int r = supplierMapper.deleteByTenantCode(smsSupplier);
        if (r == 0) {
            return new ResultModel(false, "操作删除失败");
        }
        return new ResultModel(true, "操作删除成功");
    }
}
